﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DevExpress.Web.Mvc;
using Webcar.BLL;
using Webcar.Entitys;
using Webcar.Utils;
using System.Data.Entity;
using System.Web.UI.WebControls;
using System.Threading;
using System.Data;
using EntityFramework.Extensions;
using System.IO;
namespace Webcar.Controllers
{
    
    public partial class WarningController : BaseControler
    {
        public ActionResult List()
        {
            return View();
        }

        public ActionResult ListPartialView()
        {
            return PartialView();
        }
        public ActionResult Delete(int id)
        {
            CurrentUser.EnsureAdmin();// (PermissionManager.发布预警信息);
            using (var DB = new WebcarContext())
            {
                var model = DB.warn_info.Find(id);
                if (model != null)
                {
                    DB.warn_info.Remove(model);
                    DB.SaveChanges();
                }
            }
            return Success("List", "Warning");
        }
        [HttpGet]
        public ActionResult View(int? id)
        {
            var model = WebcarDataProvider.DB.warn_info.Find(id);
            return View(model: model);
        }
        public List<string> GetAllIcons()
        {
            var image_folder = Server.MapPath(@"~\Content\warn");
            var files = Directory.GetFiles(image_folder);

            var ret = files.Select(a => System.IO.Path.GetFileName(a)).ToList();
            return ret;
        }
        [HttpGet]
        public ActionResult Edit(int? id)
        {
            warn_info model = null;
            if (id!=null)
            {
                model = WebcarDataProvider.DB.warn_info.Find(id);
            }
            if (model == null)
            {
                model = new warn_info();
                model.beg_time = DateTime.Now.Date;
                model.end_time = DateTime.Now.Date;
            }
            ViewBag.AllIcons = GetAllIcons();
            return View(model: model);
        }
        [ValidateInput(false)]
        [HttpPost]
        public ActionResult Edit(warn_info model)
        {
            CurrentUser.EnsureAdmin();// (PermissionManager.发布预警信息);
            model.icon_url = Request.Form["warn_icon"];
            if (model.title.IsNull()||model.warn_content.IsNull())
            {
                ViewBag.ErrorMessage = "请输入标题和内容。";
                return View(model);
            }
            if (model.beg_time==null|| model.end_time==null)
            {
                ViewBag.ErrorMessage = "请输入标题和内容。";
                return View(model);
            }
            if (model.beg_time>model.end_time)
            {
                ViewBag.ErrorMessage = "开始时间不能大于结束时间。";
                return View(model);
            }

            if (Request.Files.IsEmpty() == false)
            {
                model.file_url = Request.Files.UploadGsFile();
            }

            warn_info item = null;
            using (var DB = new WebcarContext())
            {
                model.OPERATOR = CurrentUser.GetLoginUserName();
                model.data_time = DateTime.Now;
                if (model.id > 0)
                {
                    item = DB.warn_info.Find(model.id);
                    string old_file = item.file_url;
                    model.CopyPropertyTo(item);
                    if (item.file_url.IsNull())
                        item.file_url = old_file;
                }
                else
                {
                    DB.warn_info.Add(model);
                }
                DB.SaveChanges();
            }
            return Success("List", "Warning");
        }
    }
}